#coding:utf-8
#!/usr/bin/env python
from flask.ext.script import Manager, Shell, Server
from flask.ext.migrate import Migrate, MigrateCommand
from app import create_app, db
from app.models import User, HomeMenu, HomePageInfo, Gallery, Menu, ArticleType, article_types, Source, Comment, Article, ArticleTypeSetting, BlogInfo, Plugin, BlogView, AboutInfo

app = create_app()
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
server = Server(host="0.0.0.0", port=5000)
manager.add_command('runserver', server)

#Global variables to jiajia2 environment:
app.jinja_env.globals['HomeMenu'] = HomeMenu
app.jinja_env.globals['HomePageInfo'] = HomePageInfo
app.jinja_env.globals['Gallery'] = Gallery
app.jinja_env.globals['Menu'] = Menu
app.jinja_env.globals['ArticleType'] = ArticleType
app.jinja_env.globals['Source'] = Source
app.jinja_env.globals['Comment'] = Comment
app.jinja_env.globals['Article'] = Article
app.jinja_env.globals['ArticleTypeSetting'] = ArticleTypeSetting
app.jinja_env.globals['BlogInfo'] = BlogInfo
app.jinja_env.globals['Plugin'] = Plugin
app.jinja_env.globals['BlogView'] = BlogView
app.jinja_env.globals['AboutInfo'] = AboutInfo


def make_shell_context():
    return dict(db=db, User=User, HomeMenu=HomeMenu, HomePageInfo=HomePageInfo, Gallery=Gallery, Menu=Menu, ArticleType=ArticleType, Source=Source, Comment=Comment, \
                       Article=Article, ArticleTypeSetting=ArticleSetting, BlogInfo=BlogInfo, Plugin=Plugin, BlogView=BlogView, AboutInfo=AboutInfo)

manager.add_command("shell", Shell(make_context=make_shell_context))

@manager.command
def deploy(deploy_type):
    from flask.ext.migrate import upgrade
    # upgrade database to the latest version
    upgrade()

    if deploy_type == 'product':
        # step_2:insert admin account
        User.insert_admin(email='blog_mini@163.com', username='blog_mini', password='blog_mini')
        HomeMenu.insert_system_homeMenu()
        HomePageInfo.insert_system_homeInfo()
        Gallery.insert_system_pictures()
        # step_1:insert basic blog info
        BlogInfo.insert_blog_info()
        # step_3:insert system default setting
        ArticleTypeSetting.insert_system_setting()
        # step_4:insert default article sources
        Source.insert_sources()
        # step_5:insert default articleType
        ArticleType.insert_system_articleType()
        # step_6:insert system plugin
        Plugin.insert_system_plugin()
        # step_7:insert blog view
        BlogView.insert_view()
        # insert aboutInfo
        AboutInfo.insert_system_aboutInfo()

    if deploy_type == 'test_data':
        HomeMenu.insert_homeMenu()
        HomePageInfo.insert_homeInfo()
        Gallery.insert_pictures()
        # step_1:insert navs
        Menu.insert_menus()
        # step_2:insert articleTypes
        ArticleType.insert_articleTypes()
        # step_3:generate random articles
        Article.generate_fake(100)
        # step_4:generate random comments
        Comment.generate_fake(300)
        # step_5:generate random replies
        Comment.generate_fake_replies(100)
        # step_4:generate random comments
        Comment.generate_fake(300)
        # insert aboutInfo
        AboutInfo.insert_aboutInfo()

if __name__ == "__main__":
    manager.run()



